package com.io.convert;

import java.io.*;

/**
 * Administrator
 *
 * @author jiangbang
 * @date 2016/6/3
 */
public class DataInputStreamDemo01 {
    public static void main(String[] args) throws IOException {
//        test1("D:/demo/2.txt");
        read("D:/demo/2.txt");
    }

    /**
     * 从文件读取数据+类型
     * @param destPath
     * @throws IOException
     */
    public static void read(String destPath) throws IOException {
        File file = new File(destPath);
        //选择流
        DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
        //操作读取的顺序与写出一致，必须存在才能读取
        double num1 = dis.readDouble();
        long num2 = dis.readLong();
        String src = dis.readUTF();
        System.out.println(num1+"-->"+src);
    }

    /**
     * 数据+类型输出到文件
     * @param destPath
     * @throws IOException
     */
    public static void test1(String destPath) throws IOException {
        double point = 2.5;
        long num = 100L;
        String str = "数据类型";

        //创建源
        File dest = new File(destPath);
        //选择流  dataOutputStream
        DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dest)));
        //操作写出的顺序为读取准备
        dos.writeDouble(point);
        dos.writeLong(num);
        dos.writeUTF(str);
        dos.flush();
        dos.close();
    }
}
